﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeBuilder.Repository
{
    public class CreateIBaseRepository
    {
        private string tableName;
        private string fileDir;
        public CreateIBaseRepository(string tableName, string fileDir)
        {
            this.tableName = tableName;
            this.fileDir = fileDir;
        }

        public void Run()
        {
            var entityName = Helper.GetPascalCase(tableName);
            var fileName = $"IBaseRepository";
            IList<string> contents = new List<string>();
            contents.Add($"using Hualong.TestWorldPro.Entities;");
            contents.Add($"");
            contents.Add($"namespace Hualong.TestWorldPro.Repository");
            contents.Add("{");
            contents.Add($"    public interface {fileName}");
            contents.Add("    {");
            contents.Add($"        Task<int> DeleteItemAsync(object item);");
            contents.Add($"        Task<int> DeleteItemsAsync(IEnumerable items);");
            contents.Add($"        Task<int> InsertAsync(object item);");
            contents.Add($"        Task<int> InsertAllAsync(IEnumerable items);");
            contents.Add($"        Task<int> UpdateAsync(object item);");
            contents.Add($"        Task<int> UpdateAllAsync(IEnumerable items);");
            contents.Add("    }");
            contents.Add("}");
            string filePath = Path.Combine(fileDir, fileName);
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            filePath = Path.Combine(filePath, $"{fileName}.cs");
            if (File.Exists(filePath))
            { File.Delete(filePath); }
            File.WriteAllLines(filePath, contents, Encoding.UTF8);

            Console.WriteLine($"创建:{filePath} 完成！");
        }
    }
}
